package com.example.socialwebserver.controller;

import com.example.socialwebserver.pojo.LoginDAO;
import com.example.socialwebserver.pojo.LoginDTO;
import com.example.socialwebserver.pojo.LoginPasswordDTO;
import com.example.socialwebserver.pojo.ServerResult;
import com.example.socialwebserver.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
public class LoginController {

    @Autowired
    LoginService loginService;

    @RequestMapping("/login/login")
    public ServerResult login(LoginDTO loginDTO)
    {
        Integer userId = loginService.Login(loginDTO);
        if(userId == -1)
        {
            return new ServerResult(101,"登陆失败",null);
        }
        return new ServerResult(0,"登陆成功",userId);
    }
    @RequestMapping("/login/register")
    public ServerResult register(LoginDTO loginDTO) {
        boolean register = loginService.Register(loginDTO);
        ServerResult serverResult = new ServerResult(0, "注册成功", register);
        return serverResult;
    }
    @RequestMapping("/login/changePassword")
    public ServerResult changePassword(LoginPasswordDTO userPasswordDTO){
        boolean b = loginService.ChangePassword(userPasswordDTO);
        return b==true? new ServerResult(0,"修改密码成功",b):new ServerResult(0,"修改密码失败",b);
    }
    @RequestMapping("/login/updateLogin")
    public ServerResult updateLogin(LoginDAO loginDAO){
        boolean b = loginService.UpdateById(loginDAO);
        if(b == true){
            return new ServerResult(0,"修改用户名成功",b);
        }else{
            return new ServerResult(-1,"修改用户名失败",b);
        }
    }
    @RequestMapping("/login/deleteUser")
    public ServerResult DeleteById(Integer id){
        boolean b = loginService.DeleteById(id);
        return b== true?new ServerResult(0,"删除用户信息成功",b):new ServerResult(-1,"删除用户信息失败",b);
    }
}
